﻿using System.Text;
using System.Web.Mvc;

namespace IntellectShop.Web2.Hardware.Controllers
{
	[IntellectShop.Web2.Hardware.Code.NoCache]
	[IntellectShop.Web2.Hardware.Code.HandleErrorWithELMAH]
	public class VCardResult : ActionResult
	{
		private Common.VCard _card;

		protected VCardResult() { }

		public VCardResult(Common.VCard card)
		{
			_card = card;
		}

		public override void ExecuteResult(ControllerContext context)
		{
			var response = context.HttpContext.Response;
			response.ContentType = "text/vcard";
			response.AddHeader("Content-Disposition", "attachment; fileName=" + _card.FirstName + " " + _card.LastName + ".vcf");

			var cardString = _card.ToString();
			var inputEncoding = Encoding.Default;
			var outputEncoding = Encoding.Default; //Encoding.GetEncoding("windows-1251");
			var cardBytes = inputEncoding.GetBytes(cardString);
			var outputBytes = Encoding.Convert(inputEncoding, outputEncoding, cardBytes);

			response.OutputStream.Write(outputBytes, 0, outputBytes.Length);
		}
	}
}